<template>
  <div class="app-container">
    <!-- 靠右的按钮 -->
    <page-tools>
      <template v-slot:after>
        <el-button type="primary" size="small" @click="addPer('0', 1)">添加权限</el-button>
      </template>
    </page-tools>
    <!-- 表格 -->
    <el-table border :data="list" row-key="id" default-expand-all>
      <el-table-column label="序号" type="index" width="50" align="center" />
      <el-table-column label="名称" prop="name" />
      <el-table-column label="标识" prop="code" />
      <el-table-column label="描述" prop="description" />
      <el-table-column align="center" label="操作">
        <template slot-scope="{ row }">
          <el-button v-if="row.type === 1" type="text" icon="el-icon-circle-plus-outline" @click="addPer(row.id, 2)">添加</el-button>
          <el-button type="text" icon="el-icon-edit" @click="editPer(row)">编辑</el-button>
          <el-button type="text" icon="el-icon-delete" style="color: #f56c6c" @click="delPer(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <add-per ref="AddPer" :dialog-visible.sync="dialogVisible" />
  </div>
</template>

<script>
import { delPermissionApi, getPermissionListApi } from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
import AddPer from './components/AddPer.vue'
export default {
  name: 'PermissionIndex',
  components: { AddPer },
  data() {
    return {
      list: [],
      dialogVisible: false
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取权限列表
    async getPermissionList() {
      const list = await getPermissionListApi()
      // 转换成树状图
      this.list = tranListToTreeData(list, '0')
    },
    // 两个权限不一样，需要传入pid和type传入
    addPer(pid, type) {
      this.$refs.AddPer.formData.pid = pid
      this.$refs.AddPer.formData.type = type
      this.dialogVisible = true
    },
    editPer(row) {
      this.$refs.AddPer.formData = { ...row }
      this.dialogVisible = true
    },
    async delPer(id) {
      try {
        await this.$confirm('确定要删除吗？', '提示', {
          type: 'warning'
        })
        await delPermissionApi(id)
        this.$message.success('删除成功')
        this.getPermissionList()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>

